package com.company.project.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*
 * @Description:
 * @Author:         brilliantzhao
 * @CreateDate:     2019/5/29 17:22
 * @UpdateUser:
 * @UpdateDate:     2019/5/29 17:22
 * @UpdateRemark:
 */
public class EncryptUtil {

    /*
     * @Description: 密码加密处理
     * @Author: brilliantzhao
     * @CreateDate: 2019/5/29 17:23
     * @Param:
     * @Return:
     */
    public static String passwordToHash(String password) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(password.getBytes());
            byte[] src = digest.digest();
            StringBuilder stringBuilder = new StringBuilder();
            // 字节数组转16进制字符串
            // https://my.oschina.net/u/347386/blog/182717
            for (byte aSrc : src) {
                String s = Integer.toHexString(aSrc & 0xFF);
                if (s.length() < 2) {
                    stringBuilder.append('0');
                }
                stringBuilder.append(s);
            }
            return stringBuilder.toString();
        } catch (NoSuchAlgorithmException ignore) {
        }
        return null;
    }
}
